package com.climber.spb_rabbitmq.spb_priority.controller;

import com.climber.spb_rabbitmq.spb_priority.config.RabbitMQConfig;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class MessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发布消息
     * 以队列为单位设置消息的延时
     *
     * @param content
     */
    @GetMapping("/sendPriorityMessage/{content}")
    public void sendPriorityMessage(@PathVariable("content") String content) {
//        int priority=1;
        for (int i = 1; i < 6; i++) {
            final int priority=i;
            rabbitTemplate.convertAndSend(RabbitMQConfig.X_EXCHANGE, RabbitMQConfig.ROUTING_KEY, content+priority+"  priority="+priority,  message -> {
                // 设置消息的优先级
                message.getMessageProperties().setPriority(priority);
                return message;
            });
        }

    }


}